package com.sanjiahantang.ythshopping.adapter;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RatingBar;
import android.widget.TextView;

import com.sanjiahantang.ythshopping.R;
import com.sanjiahantang.ythshopping.activity.me.EvaluateActivity;
import com.sanjiahantang.ythshopping.activity.me.ShaidanActivity;
import com.sanjiahantang.ythshopping.config.ConfigValue;
import com.sanjiahantang.ythshopping.model.EvaluateModle;
import com.sanjiahantang.shopping.uilibrary.adapter.listview.BaseAdapterHelper;
import com.sanjiahantang.shopping.uilibrary.adapter.listview.QuickAdapter;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by admin on 2017/6/27.
 */

public class EvaluateListAdapter extends QuickAdapter<EvaluateModle.EvaluateMain> {

    List<ImageView> images;

    public EvaluateListAdapter(Context context, int layoutResId) {
        super(context, layoutResId);
    }

    public EvaluateListAdapter(Context context, int layoutResId, List<EvaluateModle.EvaluateMain> data) {
        super(context, layoutResId, data);
    }

    @Override
    protected void convert(BaseAdapterHelper helper, final EvaluateModle.EvaluateMain item) {

        Picasso.with(context).load(ConfigValue.IMG_IP+item.getGoods_thumb()).into((ImageView) helper.getView(R.id.iv_evaitem_goodpic));
        helper.setText(R.id.tv_evaitme_goodname,item.getGoods_name());
        String timeStr = "";
        if(item.getBuy_time() != null){
            if(item.getBuy_time().contains(" ")){
                timeStr = item.getBuy_time().split(" ")[0];
            }else{
                timeStr = item.getBuy_time();
            }
        }
        helper.setText(R.id.tv_evaitem_time,"成交时间：" + timeStr);
        helper.setText(R.id.tv_evaitem_price,item.getShop_price());


        switch (item.getComment_state())
        {
            case "0":
                helper.getView(R.id.ll_evaitem_eva).setVisibility(View.GONE);
                helper.getView(R.id.tv_evaitem_goeva).setVisibility(View.VISIBLE);
                switch (item.getShaidan_state())
                {
                    case "0":
                        helper.getView(R.id.tv_evaitem_goshai).setVisibility(View.VISIBLE);
                        helper.getView(R.id.tv_evaitem_audit).setVisibility(View.GONE);
                        break;
                    case "1":
                        helper.getView(R.id.tv_evaitem_goshai).setVisibility(View.GONE);
                        helper.getView(R.id.tv_evaitem_audit).setVisibility(View.VISIBLE);
                        helper.setText(R.id.tv_evaitem_audit,"晒单审核中");
                        break;
                }
                break;
            case "1":
                helper.getView(R.id.ll_evaitem_eva).setVisibility(View.GONE);
                helper.getView(R.id.tv_evaitem_audit).setVisibility(View.VISIBLE);
                helper.setText(R.id.tv_evaitem_audit,"评价审核中");
                helper.getView(R.id.tv_evaitem_goeva).setVisibility(View.GONE);
                if (item.getShaidan_state().equals("0"))
                {
                    helper.getView(R.id.tv_evaitem_goshai).setVisibility(View.VISIBLE);
                }else {
                    helper.getView(R.id.tv_evaitem_goshai).setVisibility(View.GONE);
                }

                break;
            case "2":
                helper.getView(R.id.ll_evaitem_eva).setVisibility(View.VISIBLE);
                helper.getView(R.id.tv_evaitem_goeva).setVisibility(View.GONE);
                helper.getView(R.id.tv_evaitem_audit).setVisibility(View.GONE);
                RatingBar rb_star=helper.getView(R.id.rb_evaitem);
                rb_star.setRating(Float.valueOf(item.getComment().getComment_rank()));
                helper.setText(R.id.tv_evaitem_content,item.getComment().getContent());
                switch (item.getShaidan_state())
                {
                    case "0":
                        helper.getView(R.id.ll_evaitem_shaidan).setVisibility(View.GONE);
                        helper.getView(R.id.tv_evaitem_goshai).setVisibility(View.VISIBLE);
                        break;
                    case "1":
                        helper.getView(R.id.ll_evaitem_shaidan).setVisibility(View.GONE);
                        helper.getView(R.id.tv_evaitem_goshai).setVisibility(View.GONE);
                        helper.getView(R.id.tv_evaitem_audit).setVisibility(View.VISIBLE);
                        helper.setText(R.id.tv_evaitem_audit,"晒单审核中");
                        break;
                    case "2":
                        helper.getView(R.id.tv_evaitem_goshai).setVisibility(View.GONE);
                        helper.getView(R.id.ll_evaitem_shaidan).setVisibility(View.VISIBLE);
                        helper.setText(R.id.tv_evaitem_shaidan,item.getComment().getShaidan().getMessage());
                        if (item.getComment().getShaidan().getShaidan_img()!=null)

                        {
                            LinearLayout ll_shaidanpics=helper.getView(R.id.ll_evaitem_shaipic);
                            images = new ArrayList<>();
                            for (int i = 0; i < item.getComment().getShaidan().getShaidan_img().size(); i++){
                                ImageView view = new ImageView(context);
                                Picasso.with(context).load(ConfigValue.IMG_IP+item.getComment().getShaidan().getShaidan_img().get(i)).into(view);
                                images.add(view);


                            }
                            final Dialog dialog = new Dialog(context);
                            View views = LayoutInflater.from(context).inflate(R.layout.image_scaner_dialog, null);
                            dialog.addContentView(views, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
                            final ViewPager pager = (ViewPager) views.findViewById(R.id.image_scan_pager);
                            final TextView index = (TextView) views.findViewById(R.id.scan_index);
                            final ImageView iv_close= (ImageView) views.findViewById(R.id.iv_close);
                            iv_close.setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    dialog.dismiss();
                                }
                            });
                            index.setText("1/" + item.getComment().getShaidan().getShaidan_img().size());
                            pager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
                                @Override
                                public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

                                }

                                @Override
                                public void onPageSelected(int position) {
                                    index.setText(position + "/" + item.getComment().getShaidan().getShaidan_img().size());
                                }

                                @Override
                                public void onPageScrollStateChanged(int state) {

                                }
                            });
                            ImageScanAdapter adapter = new ImageScanAdapter(images);
                            pager.setAdapter(adapter);
                            dialog.setTitle("图片详情");
                            dialog.setCanceledOnTouchOutside(false);
//                            for (final String img:item.getComment().getShaidan().getShaidan_img())
                            List<String> imgs = item.getComment().getShaidan().getShaidan_img();
                            final int size = imgs.size();
                            ll_shaidanpics.removeAllViews();
                            for (int i = 0; i < size; i ++)
                            {
                                final int j = i;
                                ImageView iv_pic=new ImageView(context);
                                iv_pic.setLayoutParams(new LinearLayout.LayoutParams(80,80));
                                Picasso.with(context).load(ConfigValue.IMG_IP+imgs.get(i)).into(iv_pic);
                                ll_shaidanpics.addView(iv_pic);
                                iv_pic.setOnClickListener(new View.OnClickListener() {
                                    @Override
                                    public void onClick(View v) {
                                        dialog.show();
                                        WindowManager.LayoutParams params = dialog.getWindow().getAttributes();
                                        params.width = 800;
                                        params.height = 1200;
                                        dialog.getWindow().setAttributes(params);
                                        index.setText(j + "/" + size);
                                    }
                                });
                            }
                        }

                        break;
                    case "3":
                        helper.getView(R.id.ll_evaitem_eva).setVisibility(View.GONE);
                        helper.getView(R.id.tv_evaitem_goshai).setVisibility(View.GONE);
                        helper.getView(R.id.tv_evaitem_audit).setVisibility(View.VISIBLE);
                        helper.getView(R.id.tv_evaitem_goeva).setVisibility(View.GONE);
                        helper.setText(R.id.tv_evaitem_audit,"评价过期");
                        break;

                }
                break;

        }

        helper.getView(R.id.tv_evaitem_goeva).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                Intent evaintent =new Intent(context, EvaluateActivity.class);
                Bundle bundle=new Bundle();
                bundle.putSerializable("evamain",item);
                evaintent.putExtras(bundle);
                context.startActivity(evaintent);
            }
        });
        helper.getView(R.id.tv_evaitem_goshai).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent shaiIntent=new Intent(context, ShaidanActivity.class);
                Bundle bundle=new Bundle();
                bundle.putSerializable("shaimain",item);
                shaiIntent.putExtras(bundle);
                context.startActivity(shaiIntent);
            }
        });
    }
}
